package cn.enilu.flash.api.controller.shop;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.entity.shop.Category;
import cn.enilu.flash.bean.entity.shop.CategoryFirst;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.exception.ApplicationExceptionEnum;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.shop.CategoryFirstVO;
import cn.enilu.flash.service.shop.CategoryFirstService;
import cn.enilu.flash.service.shop.CategoryService;
import cn.enilu.flash.utils.Lists;
import cn.enilu.flash.utils.factory.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 一级分类<br>
 *
 * @className: CategoryFirstController
 * @package: cn.enilu.flash.api.controller.shop
 * @author: wuchaohong
 * @date: 2020/8/6 15:12
 */

@RestController
@RequestMapping("/shop/categoryFirst")
public class CategoryFirstController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private CategoryFirstService categoryFirstService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list() {
        Page<CategoryFirst> page = new PageFactory<CategoryFirst>().defaultPage();
//        page.addFilter("name", SearchFilter.Operator.LIKE,name);
        page.setSort(Sort.by(Sort.Direction.DESC, "flag"));
        page = categoryFirstService.queryPage(page);
        return Rets.success(page);
    }

    @RequestMapping(method = RequestMethod.POST)
    @BussinessLog(value = "新增或修改一级分类")
    public Object save(@RequestBody CategoryFirst categoryFirst) {
        if (null == categoryFirst.getId()) {
            categoryFirstService.insert(categoryFirst);
        } else {
            categoryFirstService.update(categoryFirst);
        }
        return Rets.success();
    }

    @RequestMapping(value = "/updateFlag", method = RequestMethod.POST)
    @BussinessLog(value = "开启/关闭flag", key = "id")
    public Object remove(@RequestParam("id") Long id, @RequestParam("flag") String flag) {
        if (id == null) {
            throw new ApplicationException(ApplicationExceptionEnum.REQUEST_NULL);
        }
        CategoryFirst categoryFirst = new CategoryFirst();
        categoryFirst.setId(id);
        categoryFirst.setFlag(flag);
        categoryFirstService.update(categoryFirst);
        return Rets.success();
    }

    @RequestMapping(value = "/getOpenFirstCategory", method = RequestMethod.GET)
    @BussinessLog(value = "获取所有开启的一级分类")
    public Object getOpenFirstCategory() {
        List<SearchFilter> filters = Lists.newArrayList();
        filters.add(SearchFilter.build("flag", 1));
        List<CategoryFirst> categoryFirstList = categoryFirstService.queryAll(filters);
        return Rets.success(categoryFirstList);
    }

    @RequestMapping(value = "/getFirstCategoryAndSecond", method = RequestMethod.GET)
    @BussinessLog(value = "获取所有开启的一级分类,并带出二级分类")
    public Object getFirstCategoryAndSecond() {
        List<SearchFilter> filters = Lists.newArrayList();
        filters.add(SearchFilter.build("flag", 1));
        List<CategoryFirst> categoryFirstList = categoryFirstService.queryAll(filters);
        List<CategoryFirstVO> categoryFirstVOList = new ArrayList<>();

        categoryFirstList.forEach(l->{
            CategoryFirstVO categoryFirstVO = new CategoryFirstVO();
            categoryFirstVO.setName(l.getName());
            categoryFirstVO.setId(l.getId());
            List<SearchFilter> filterCategory = Lists.newArrayList();
            filterCategory.add(SearchFilter.build("fistId",l.getId()));
            List<Category> categoryList = categoryService.queryAll(filterCategory);
            categoryFirstVO.setCategoryList(categoryList);
            categoryFirstVOList.add(categoryFirstVO);
        });
        return Rets.success(categoryFirstVOList);
    }
}
